package com.touchapk.javafx;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;



/**
 * @description
 * @author yangkeyun
 * @email coyun@qq.com
 * @date 2016年8月7日
 * @version V1.0
 */
public class NewProjectController implements DialogController {

	private MainController mainController;

	public NewProjectController() {
	}

	public NewProjectController(MainController mainController) {
		this.mainController = mainController;
	}

	private FXMLDialog dialog;

	@Override
	public void setDialog(FXMLDialog dialog) {
		this.dialog = dialog;
	}

	@FXML
	private TextField textField;
	@FXML
	private Button okButton;
	
	@FXML
	public void okFunc() {
		
		String name=textField.getText();
		if("".equals(name.trim())){
			MTooltip.show(dialog, textField, this.mainController.getI18nBundle().getString("menu.msg.projectname"));
			return;
		}
		 Platform.runLater(new Runnable() {
             @Override
             public void run() {
            	 createProject(name);
         		
         		File rfile = new File(mainController.getPath()+"\\"+name);

         		FileTreeItem rootsitem = new FileTreeItem(rfile,false);
         		
         		mainController.getTreeView().getRoot().getChildren().add(rootsitem);
             }
		 });
		
		
		this.dialog.close();
		
	}

	
	@FXML
	public void cancelFunc() {
		this.dialog.close();
	}

	/**
	 * Initializes the controller class.
	 */
	@FXML
	// This method is called by the FXMLLoader when initialization is complete
	void initialize() {
		textField.textProperty().addListener(new ChangeListener() {
			@Override
			public void changed(ObservableValue observable, Object oldValue, Object newValue) {
				String nPath=mainController.getPath()+"\\"+newValue.toString();
				File nFile=new File(nPath);
				if(nFile.exists()){
					okButton.setDisable(true);
				}else{
					okButton.setDisable(false);
				}
			}
		});
	}
	

	public void createProject(String name){
		String nPath=mainController.getPath()+"\\"+name;
		File nFile=new File(nPath);
		if(!nFile.exists()){
			nFile.mkdir();
		}
		String lPath=nPath+"\\hello.lua";
		File lFile=new File(lPath);
		
		try {
			lFile.createNewFile();
			
			FileOutputStream out=new FileOutputStream(lFile);
			BufferedWriter rd = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
			 rd.write(getCode());
			
			 rd.close();
			 out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String pPath=nPath+"\\.project";
		File pFile=new File(pPath);
		
		try {
			pFile.createNewFile();
			FileOutputStream out=new FileOutputStream(pFile);
			out.write(("projectName="+name+"\nstatus=1").getBytes());
			out.flush();
			out.close();
		} catch (Exception e) { 
			e.printStackTrace();
		}
		
	}
	
	 private String getCode(){
		 
		return String.join("\n", new String[] {
	 
		        "",
		        "    --[[",
		        "       "+mainController.getI18nBundle().getString("comment.multiline"),
		        "    --]]",
		        "    --"+mainController.getI18nBundle().getString("comment.singleline"),
		        "    ",
		        "sayHello=function()",
		        "	print(\"hello world!\")",
		        "end",
		        "",
		        "sayHello()",
		        "",
		        "",
		        "",
		        ""
		    });
	
	 }
}
